package com.dao.impl;

import com.dao.IPaymentDao;
import com.model.PaymentModel;
import com.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class PaymentDaoImpl implements IPaymentDao {
    private JdbcTemplate jt = new JdbcTemplate(JdbcUtils.getDs());

    @Override
    public PaymentModel createPayment(PaymentModel payment) throws Exception {
        String sql = "INSERT INTO payments (order_id, payment_method, amount, status, transaction_id, payment_time, card_info) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?)";
        
        jt.update(sql,
            payment.getOrderId(),
            payment.getPaymentMethod(),
            payment.getAmount(),
            payment.getStatus(),
            payment.getTransactionId(),
            payment.getPaymentTime(),
            payment.getCardInfo()
        );
        
        return getPaymentByOrderId(payment.getOrderId());
    }

    @Override
    public PaymentModel getPaymentByOrderId(Integer orderId) throws Exception {
        String sql = "SELECT * FROM payments WHERE order_id = ?";
        return jt.queryForObject(sql, new BeanPropertyRowMapper<>(PaymentModel.class), orderId);
    }

    @Override
    public void updatePaymentStatus(Integer paymentId, String status) throws Exception {
        String sql = "UPDATE payments SET status = ? WHERE payment_id = ?";
        jt.update(sql, status, paymentId);
    }
} 